//
//  SCShopCartController.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/28.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCShopCartController.h"
#import "ZJScrollPageView.h"
#import "SCIntegralCartController.h"
#import "SCGoodsCartController.h"
#import "SCTakeawayCartController.h"

@interface SCShopCartController ()<ZJScrollPageViewDelegate>

@property (assign, nonatomic) NSInteger scrollIndex;//滑动到的下标
@property (strong, nonatomic) NSArray   *titles;
@property (strong, nonatomic) UIButton  *editBtn;
@property (assign, nonatomic) BOOL      editbool;//是否编辑


@end

@implementation SCShopCartController

- (UIButton *)editBtn{
    if (!_editBtn) {
        _editBtn = [UIButton createButtonFrame:CGRectMake(0, 0, 44, 44) groundColor:[UIColor whiteColor] title:@"编辑" titleColor:[UIColor blackColor] font:SCFont(15) target:self action:@selector(clickEditItem)];
    }
    return _editBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化
    [self setupSegmentView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.editBtn];
}
- (void)clickEditItem{
    
    if (self.editbool == 0) {
        self.editbool = 1;
        [self.editBtn setTitle:@"完成" forState:0];
    }else{
        self.editbool = 0;
        [self.editBtn setTitle:@"编辑" forState:0];
    }
    NSDictionary *dict = @{@"editbool":@(self.editbool),
                           @"scrollIndex":@(self.scrollIndex)};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ClickEditButton" object:dict];
}
- (void)setupSegmentView{
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    style.showLine = YES;//显示遮盖
    style.scrollLineColor = RGB(255, 155, 23);
    style.titleFont = SCFont(14);
    style.segmentHeight = 38;
    // 当标题(和图片)宽度总和小于ZJScrollPageView的宽度的时候, 标题会自适应宽度
    style.autoAdjustTitlesWidth = YES;
    style.contentViewBounces = NO;
    style.gradualChangeTitleColor = YES;// 颜色渐变
    style.normalTitleColor = RGB(96, 96, 96);
    style.selectedTitleColor = RGB(255, 155, 23);
    self.titles = @[@"福豆商城", @"商城", @"外卖"];
    // 初始化
    ZJScrollPageView *scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - kTopHeight) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:scrollPageView];
    
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 0.5)];
    lineView.backgroundColor = RGBA(239, 239, 239, 239);
    [self.view addSubview:lineView];
}
#pragma mark - 代理
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}
- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index{
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = self.childVcs[index];
    }
    return childVc;
}
- (NSArray *)childVcs{
    
    WeakSelf;
    
    SCIntegralCartController *integralCartVc = [[SCIntegralCartController alloc]init];
    integralCartVc.isGiftApp = YES;
    integralCartVc.scrollWithIndex = ^(NSInteger Index) {
        weakSelf.scrollIndex = Index;
        weakSelf.editbool = NO;
        [weakSelf.editBtn setTitle:@"编辑" forState:0];
    };
    
    SCIntegralCartController *goodsCartVc = [[SCIntegralCartController alloc]init];
    integralCartVc.scrollWithIndex = ^(NSInteger Index) {
        weakSelf.scrollIndex = Index;
        weakSelf.editbool = NO;
        [weakSelf.editBtn setTitle:@"编辑" forState:0];
    };
    
    SCTakeawayCartController *takeawayCartVc = [[SCTakeawayCartController alloc]init];
    takeawayCartVc.scrollWithIndex = ^(NSInteger Index) {
        weakSelf.scrollIndex = Index;
        weakSelf.editbool = NO;
        [weakSelf.editBtn setTitle:@"编辑" forState:0];
    };

    NSArray *childVcs = @[integralCartVc, goodsCartVc, takeawayCartVc];
    return childVcs;
}

@end
